<?php
class m_mata_pelajaran extends Model{
    var $table = "mata_pelajaran";
    
    function getCountRows(){
        return $this->db->count_all($this->table);
    }
    function getListMataPelajaran($offset = NULL,$limit = NULL){
        $query = $this->db->query('select m.*,k.nama as kelas,t.nama as tingkat_pendidikan from mata_pelajaran m join kelas k on m.id_kelas = k.id join tingkat_pendidikan t on k.id_tingkat_pendidikan = t.id limit '.$offset.','.$limit.'');
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getMataPelajaranById($id = NULL){
        if($id != NULL){
            $where = "where m.id = '$id'";
        }else $where = "";
        
        $query = $this->db->query("select m.*,k.nama as kelas,t.nama as tingkat_pendidikan from mata_pelajaran m join kelas k on m.id_kelas = k.id join tingkat_pendidikan t on k.id_tingkat_pendidikan = t.id $where");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function getMataPelajaranByKelas($id_kelas,$nama){
        $query = $this->db->query("select m.*,k.nama as kelas,t.nama as tingkat_pendidikan from mata_pelajaran m 
                                  join kelas k on m.id_kelas = k.id
                                  join tingkat_pendidikan t on k.id_tingkat_pendidikan = t.id
                                  where m.id_kelas = '$id_kelas' and locate('$nama',m.nama) > 0 order by locate('$nama',m.nama)");
        if($query->num_rows() > 0){
            $result = $query->result_array();
        }else $result = array();
        
        return $result;
    }
    function addMataPelajaran($data){
        $insert = $this->db->insert($this->table,$data);
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editMataPelajaran($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function deleteMataPelajaran($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
}
?>
